﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

using Rison.BusinessFacade.Common;
using Rison.DataAccess.Data.Biz;
using Rison.DataAccess.Common;
using Rison.BusinessFacade.Helper;
using Rison.BusinessFacade.Entity.Biz;

namespace Rison.BusinessFacade.Facade.Biz
{
    public sealed class InfomationFacade : FacadeBase
    {
        private InfoCategory _category = InfoCategory.None;
        
        public InfomationFacade() { }

        public InfomationFacade(InfoCategory category)
        {
            _category = category;
        }

        protected override DataAccessBase dbObject
        {
            get
            {
                if (dbHelper == null)
                    dbHelper = new InfomationDb();

                return dbHelper;
            }
        }

        protected override string identityFieldName
        {
            get { return "InfoID"; }
        }


        protected override List<SqlField> GetSqlFieldCollection(object value, bool getIdentityField)
        {
            List<SqlField> fields = new List<SqlField>();
            if (getIdentityField)
                fields.Add(new SqlField("InfoId", (value as Infomation).InfoId, true, false));
            fields.Add(new SqlField("Category", (value as Infomation).Category));
            fields.Add(new SqlField("Subject", (value as Infomation).Subject));
            fields.Add(new SqlField("IsImageNews", (value as Infomation).IsImageNews));
            fields.Add(new SqlField("ImageUrl", (value as Infomation).ImageUrl));
            fields.Add(new SqlField("Content", (value as Infomation).Content));
            fields.Add(new SqlField("Creator", (value as Infomation).Creator));
            fields.Add(new SqlField("CreateTime", (value as Infomation).CreateTime, false));

            return fields;
        }

        protected override object SetPropertiesValue(DataRow row)
        {
            Infomation obj = new Infomation();
            obj.InfoId = Convert.ToInt64(row["InfoId"]);
            obj.Category = Convert.ToInt32(row["Category"]);
            obj.Subject = row["Subject"].ToString();
            obj.IsImageNews = Convert.ToBoolean(row["IsImageNews"]);
            obj.ImageUrl = StringHelper.GetValue(row, "ImageUrl");
            obj.Content = StringHelper.GetValue(row, "Content");
            obj.Creator = Convert.ToInt32(row["Creator"]);
            obj.CreateTime = StringHelper.GetDateTimeValue(row, "CreateTime");

            return obj;
        }


        public DataSet GetTopNumInfos(int topNum)
        {
            return GetTopNumInfos(_category, topNum);
        }

        public DataSet GetTopNumInfos(int topNum, int subjectLength)
        {
            return GetTopNumInfos(_category, topNum, subjectLength);
        }

        public DataSet GetTopNumInfos(InfoCategory categoryId, int topNum)
        {
            return (dbObject as InfomationDb).GetTopNumInfos(Convert.ToInt32(categoryId), topNum);
        }

        public DataSet GetTopNumInfos(InfoCategory categoryId, int topNum, int subjectLength)
        {
            return (dbObject as InfomationDb).GetTopNumInfos(Convert.ToInt32(categoryId), topNum, subjectLength);
        }



        public long Save(Infomation objInfo, List<SysAttachment> attachments)
        {
            long returnValue = 1;

            dbObject.BeginTransaction();
            try
            {
                returnValue = base.Save(objInfo);
                if (returnValue > 0)
                {
                    if (attachments != null && attachments.Count > 0)
                    {
                        SysAttachmentFacade facade = new SysAttachmentFacade("Biz_InfomationAtt", dbObject);
                        returnValue = facade.SaveAttachment(attachments, returnValue);
                        facade = null;
                    }
                }

                if (returnValue >= 1)
                    dbObject.CommitTransaction();
                else
                    dbObject.RollBackTransaction();
            }
            catch
            {
                dbObject.RollBackTransaction();
            }

            return returnValue;
        }

        public int Update(Infomation objInfo, List<SysAttachment> attachments)
        {
            int returnValue = 1;

            dbObject.BeginTransaction();
            try
            {
                returnValue = base.Update(objInfo);
                if (returnValue > 0)
                {
                    if (attachments != null && attachments.Count > 0)
                    {
                        SysAttachmentFacade facade = new SysAttachmentFacade("Biz_InfomationAtt", dbObject);
                        returnValue = facade.SaveAttachment(attachments, objInfo.InfoId);
                        facade = null;
                    }
                }

                if (returnValue >= 1)
                    dbObject.CommitTransaction();
                else
                    dbObject.RollBackTransaction();
            }
            catch
            {
                dbObject.RollBackTransaction();
            }

            return returnValue;
        }


    }
}
